package com.yuanian.timingwheel.respository.po;

import com.yuanian.timingwheel.infrastructure.DateUtils;
import com.yuanian.timingwheel.model.task.AttachTask;
import com.yuanian.timingwheel.model.task.base.BaseTask;
import com.yuanian.timingwheel.model.task.method.TaskMethod;
import com.yuanian.timingwheel.respository.po.query.QTaskEntity;
import io.ebean.Model;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.SoftDelete;
import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "TP_TW_TASK")
@MappedSuperclass
public class TaskEntity extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String businessId;

    private String taskDesc;
    //任务类型
    private int type;
    //间隔秒数
    private Long executionInterval;
    //执行次数
    private Long executionRounds;
    //下次执行时间
    private Date executionTime;
    //已经执行次数
    private Long executedRounds;

    //执行的方法
    @DbJson
    private TaskMethod taskMethod;

    //附属任务
    @DbJson
    private List<AttachTask> attachTaskList;

    private boolean isSuccess;

    private String exceptionMsg;

    private Date createTime;

    private Date updateTime;

    @SoftDelete
    private boolean deleted;

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public String getTaskDesc() {
        return taskDesc;
    }

    public void setTaskDesc(String taskDesc) {
        this.taskDesc = taskDesc;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Long getExecutionInterval() {
        return executionInterval;
    }

    public void setExecutionInterval(Long executionInterval) {
        this.executionInterval = executionInterval;
    }

    public Long getExecutionRounds() {
        return executionRounds;
    }

    public void setExecutionRounds(Long executionRounds) {
        this.executionRounds = executionRounds;
    }

    public Date getExecutionTime() {
        return executionTime;
    }

    public void setExecutionTime(Date executionTime) {
        this.executionTime = executionTime;
    }

    public Long getExecutedRounds() {
        return executedRounds;
    }

    public void setExecutedRounds(Long executedRounds) {
        this.executedRounds = executedRounds;
    }

    public TaskMethod getTaskMethod() {
        return taskMethod;
    }

    public void setTaskMethod(TaskMethod taskMethod) {
        this.taskMethod = taskMethod;
    }

    public List<AttachTask> getAttachTaskList() {
        return attachTaskList;
    }

    public void setAttachTaskList(List<AttachTask> attachTaskList) {
        this.attachTaskList = attachTaskList;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public String getExceptionMsg() {
        return exceptionMsg;
    }

    public void setExceptionMsg(String exceptionMsg) {
        this.exceptionMsg = exceptionMsg;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public static void  save(BaseTask task) {
        TaskEntity taskEntity = new TaskEntity();
        System.out.println("----------save-----------------");
        taskEntity.setBusinessId(task.getBusinessId());
        taskEntity.setTaskDesc(task.getTaskDesc());
        taskEntity.setType(1);
        taskEntity.setExecutionInterval(task.getTaskInfo().getExecutionInterval());
        taskEntity.setExecutedRounds(task.getTaskInfo().getExecutedRounds());
        taskEntity.setExecutionRounds(task.getTaskInfo().getExecutionRounds());
        taskEntity.setExecutionTime(task.getTaskInfo().getExecutionTime());
        taskEntity.setTaskMethod(task.getTaskMethod());
        taskEntity.setAttachTaskList(task.getAttachTaskList());
        taskEntity.setCreateTime(new Date());
        taskEntity.save();
    }

    /**
     * 获取当前23:59:59之前的任务
     * @return
     */
    public static List<TaskEntity> getEndOfDayTask() {
        QTaskEntity qTaskEntity = new QTaskEntity();
        List<TaskEntity> list = qTaskEntity.executionTime.before(DateUtils.getEndOfDay(new Date())).findList();
        return list;
    }

    public static void update(BaseTask baseTask) {
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setId(baseTask.getId());
        taskEntity.setExecutedRounds(baseTask.getTaskInfo().getExecutedRounds());
        taskEntity.setUpdateTime(new Date());
        taskEntity.setExecutionTime(baseTask.getTaskInfo().getExecutionTime());
        taskEntity.update();
    }
}
